#!/usr/bin/env python3

# MadHelix is a Java Swing-based GUI frontend for SoundHelix.
# Copyright (C) 2017-2019, 2023 UltrasonicMadness
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 only,
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import json
from datetime import datetime, timezone
from time import mktime
from xml.etree import ElementTree

if __name__ == "__main__":
    json_input_path = "history.json"
    xml_output_path = "history.xml"

    madhelix_history = ElementTree.Element("madhelix-history")

    with open(json_input_path) as json_input:
        for json_entry in json.load(json_input):
            current_date = json_entry["date"]
            local_time = datetime(current_date["year"], current_date["month"],
                    current_date["day"], hour=current_date["hour"],
                    minute=current_date["minute"],
                    second=current_date["second"])

            timestamp = mktime(local_time.astimezone(timezone.utc).timetuple())

            xml_entry = ElementTree.Element("entry", name=json_entry["name"],
                style=json_entry["style"], length=str(json_entry["length"]),
                timestamp=str(int(timestamp)))

            madhelix_history.append(xml_entry)

    output_tree = ElementTree.ElementTree(madhelix_history)
    ElementTree.indent(output_tree)
    output_tree.write(xml_output_path)
